type BusClass<T> = {
  emit: (name: T) => void;
  on: (name: T, callback: Function) => void;
};
type BusParams = string | number | symbol;
type List = {
  [key: BusParams]: Array<Function>;
};
class Bus<T extends BusParams> implements BusClass<T> {
  list: List;
  constructor() {
    this.list = {};
  }
  emit(name: T, ...args: Array<any>) {
    const eventName: Array<Function> = this.list[name];
    eventName.forEach((ev) => {
      ev.apply(this, args);
    });
  }
  on(name: T, callback: Function) {
    const fn: Array<Function> = this.list[name] || [];
    fn.push(callback);
    this.list[name] = fn;
  }
}

export default new Bus();
